package com.xuanyang.dormrepair.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xuanyang.dormrepair.config.hook.AdminLogin;
import com.xuanyang.dormrepair.model.entity.Maintainer;
import com.xuanyang.dormrepair.service.MaintainerService;
import com.xuanyang.dormrepair.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2019/5/23 8:50
 * @DESCRIBE 维修员管理
 */
@AdminLogin
@RestController
@RequestMapping("/admin/maintainer")
public class MaintainerController {

    @Autowired
    private MaintainerService maintainerService;

    @PostMapping("/list")
    public RestResponse list(String hno) {
        List<Maintainer> maintainers = maintainerService.listByHNo(hno);
        if (CollectionUtils.isNotEmpty(maintainers)) {
            return RestResponse.ok(maintainers);
        }
        return RestResponse.fail();
    }

    @PostMapping("/page")
    public RestResponse page(int page, int limit, Maintainer query) {
        IPage<Maintainer> iPage = maintainerService.page(page, limit, query);
        if (iPage.getTotal() > 0) {
            return RestResponse.ok(iPage);
        }
        return RestResponse.fail();
    }

    @PostMapping("/add")
    public RestResponse add(Maintainer maintainer, @RequestParam("fls[]") String[] fls) {
        maintainer.setFs(fls);
        if (maintainerService.add(maintainer)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/del/{id}")
    public RestResponse del(@PathVariable Integer id) {
        if (maintainerService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/change")
    public RestResponse change(Maintainer maintainer, @RequestParam("fls[]") String[] fls) {
        maintainer.setFs(fls);
        if (maintainerService.update(maintainer)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
